<?php

namespace api\wechat;

use utils\base64_to_file;
use utils\oss;

class menu
{

    //获取每日菜单评价数据
    public function getMenuDayRecordList($data)
    {
        $type = (int)$data['type'];
        $date = $data['date'];
        $openid = $data['openid'];
        $page = (int)$data['page'];
        $size = (int)$data['size'];
        $start = ($page - 1) * $size;

        $con = con();
        $start_time = strtotime($date);
        $end_time = $start_time + 86399;

        $user = mysqli_query($con, "select * from user where openid='$openid'");
        $user = mysqli_fetch_all($user, true);
        if (count($user) == 0) {
            mysqli_close($con);
            return out_error('非法用户！');
        }
        $user_id = (int)$user[0]['id'];

        $sql = "select * from menu_day_record where type=$type and create_time between $start_time and $end_time";
        $data = mysqli_query($con, $sql . " order by id DESC limit $start,$size");
        $data = mysqli_fetch_all($data, true);

        $total = mysqli_query($con, $sql);
        $total = mysqli_num_rows($total);

        if (count($data) > 0) {
            $ids = array_column($data, 'id');
            $ids = implode(',', $ids);
            $coment = mysqli_query($con, "select * from menu_day_record_coment where menu_day_record_id in ($ids) and user_id=$user_id and is_delete=1 and status=1");
            $coment = mysqli_fetch_all($coment, true);
        } else {
            $coment = [];
        }

        mysqli_close($con);

        for ($i = 0; $i < count($data); $i++) {
            $data[$i]['has_coment'] = false;
            $data[$i]['has_coment_text'] = '未评价';
            $data[$i]['coment'] = null;
            $data[$i]['date'] = date('Y-m-d', $data[$i]['create_time']);
            for ($o = 0; $o < count($coment); $o++) {
                if ($data[$i]['id'] == $coment[$o]['menu_day_record_id']) {
                    $data[$i]['has_coment'] = true;
                    $coment[$o]['type']=(int)$coment[$o]['type'];
                    $data[$i]['coment'] = $coment[$o];
                    if (empty($data[$i]['coment']['img_url'])) {
                        $data[$i]['coment']['img_url'] = [];
                    } else {
                        $data[$i]['coment']['img_url'] = explode(',', $data[$i]['coment']['img_url']);
                    }
                    if ($data[$i]['coment']['type'] === '1') {
                        $data[$i]['has_coment_text'] = '好评';
                    } else {
                        $data[$i]['has_coment_text'] = '差评';
                    }
                }
            }
            if (empty($data[$i]['goods_url'])) {
                $data[$i]['goods_url'] = ['https://luopingcn.oss-cn-chengdu.aliyuncs.com/no_goods.png'];
            } else {
                $data[$i]['goods_url'] = explode(',', $data[$i]['goods_url']);
            }
            switch ($data[$i]['type']) {
                case '1'://早餐
                    $data[$i]['type_text'] = '早餐';
                    break;
                case '2'://午餐
                    $data[$i]['type_text'] = '午餐';
                    break;
                case '3'://晚餐
                    $data[$i]['type_text'] = '晚餐';
                    break;
                default:
                    $data[$i]['type_text'] = '';
                    break;
            }
        }

        return out_right([
            'data' => $data,
            'page' => $page,
            'size' => $size,
            'total' => $total,
            'total_page' => ceil($total / $size)
        ]);
    }

    //评价每日菜单
    public function up($data){
        $con=con();
        $openid=$data['openid'];
        $content=$data['content'];
        $img=$data['img'];
        $type=(int)$data['type'];
        $menu_day_record_id = (int)$data['menu_day_record_id'];
        $date=date('Y-m-d');
        $url=[];

        $user_id=mysqli_query($con,"select * from user where openid='$openid' ");
        $user_id=mysqli_fetch_all($user_id,true);
        if(count($user_id)==0){
            mysqli_close($con);
            return out_error('查无此人！');
        }
        $user_id=(int)$user_id[0]['id'];

        for($i=0;$i<count($img);$i++){
            $file=base64_to_file::get("",$img[$i]["file_name"],$img[$i]["file"]);
            $res=oss::upload("menu/$date/".$file["name"],$file["path"]);
            $url[]=$res["info"]["url"];
        }
        if(empty($url)){
            $url='';
        }else{
            $url=implode(',',$url);
        }
        $time=time();
        $res=mysqli_query($con,"insert into menu_day_record_coment(menu_day_record_id,user_id,type,img_url,content,create_time,update_time) values($menu_day_record_id,$user_id,$type,'$url','$content',$time,$time)");
        mysqli_close($con);
        if($res){
            return out_right();
        }else{
            return out_error('评价失败！');
        }
    }

}